﻿using Microsoft.Practices.ServiceLocation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Youtube_HD.Model;
using Youtube_HD.Services;
using Youtube_HD.View;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace Youtube_HD.Controls
{
    public sealed partial class ControlTeamplateData : UserControl
    {
        static bool isLoading = true;
        public ControlTeamplateData()
        {
            this.InitializeComponent();
            this.Loaded += ControlTeamplateData_Loaded;
          
        }

        void ControlTeamplateData_Loaded(object sender, RoutedEventArgs e)
        {
            DetectScrollState temp = new DetectScrollState(this.listview, true);
            temp.ScrollingDownToEnd += temp_ScrollingDownToEnd;

        }
        private IHomeViewModel homeViewModel
        {
            get { return ServiceLocator.Current.GetInstance<IHomeViewModel>(); }
        }
        async void temp_ScrollingDownToEnd()
        {

            if (isLoading)
            {
                isLoading = false;
                await homeViewModel.GetTrailersAsync();
                isLoading = true;
            }
        }

        private void Videos_ItemClick(object sender, ItemClickEventArgs e)
        {
            homeViewModel.LoadingComplete = true;
            var getDat = (YoutubeVideo)e.ClickedItem;
            homeViewModel.LoadingComplete = false;
            Frame fm = Window.Current.Content as Frame;
            fm.Navigate(typeof(DetailPage), getDat);
        }
    }
}
